//
//  UIViewController+Extension.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/10.
//

import UIKit

extension UIViewController {
    #if DEBUG
    // MARK: - DEBUG下摇晃开启FLEX
    open override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        super.motionBegan(motion, with: event)
        if motion == .motionShake {
            FLEXManager.shared.showExplorer()
        }
    }
    #endif
    
    // MARK: - push到下个viewController并移除上一个viewController
    func pushAndRemovePreviousViewController(_ vc : UIViewController) {
        if navigationController == nil || navigationController!.viewControllers.count == 0 { return }
        let viewControllers : [UIViewController] = navigationController!.viewControllers
        var newViewControllers : [UIViewController] = []
        for index in 0..<(viewControllers.count - 1) {
            newViewControllers.append(viewControllers[index])
        }
        newViewControllers.append(vc)
        navigationController!.setViewControllers(newViewControllers, animated: true)
    }

    // MARK: - 从底部弹出自定义view
    func showCustomView(_ view : UIView, completion : (()->Void)? = nil) {
        let layout = KLCPopupLayout(horizontal: .center, vertical: .bottom)
        let popup : KLCPopup = KLCPopup(contentView: view, showType: .slideInFromBottom, dismissType: .slideOutToBottom, maskType: .dimmed, dismissOnBackgroundTouch: true, dismissOnContentTouch: false)
        popup.show(with: layout)
        popup.didFinishShowingCompletion = {
            if completion != nil {
                completion!()
            }
        }
    }
}


